package com.salim.leetcode.$4;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-7-26
 * Time: 下午10:25
 * To change this template use File | Settings | File Templates.
 */
public class Median {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int forLength = nums1.length + nums2.length;
        int index1 = 0;
        int index2 = 0;
        forLength = (forLength/2) +1;
        //forLength = (nums1.length + nums2.length)%2 == 0 ? (forLength/2)+1 : forLength/2;
        forLength = forLength == 0 ? 1 : forLength;
        int[] comboNums = new int[forLength];
        //合并数组
        for(int i=0;i<forLength;i++){

            if(index1 >= nums1.length){
                comboNums[i] = nums2[index2];
                index2++;
            }else if(index2 >= nums2.length){
                comboNums[i] = nums1[index1];
                index1++;
            }else if(nums1[index1] <= nums2[index2]){
                comboNums[i] = nums1[index1];
                index1++;
            }else {
                comboNums[i] = nums2[index2];
                index2++;
            }
        }
/*        for(int j=0;j<comboNums.length;j++){
            System.out.println(comboNums[j]);
        }*/
        return (nums1.length + nums2.length)%2 == 0?(double)(comboNums[comboNums.length-1]+comboNums[comboNums.length-2])/2:comboNums[comboNums.length-1];
    }

    public static void main(String[] args){
        int[] a = {};
        int[] b = {1,2,3,4,5,6};
        Median c = new Median();
        System.out.println(c.findMedianSortedArrays(a,b));
    }
}
